package com.okok.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.shiro.entity.IdEntity;
import com.shiro.entity.Role;
import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.util.CollectionUtils;

import java.util.*;

public class User extends IdEntity {
    private String loginName;
    private String loginPwd;
    private String name;
    private String salt;
    private String email;
    private String app;

    // 注册日期
    private Date registerDate;

    // 明文密码
    private String plainPassword;

    //角色
    @DBRef
    private List<Role> roles;

    @Transient
    private Set<String> permissions = new HashSet<String>();

    @Transient
    private Set<String> strRoles = new HashSet<String>();

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getLoginPwd() {
        return loginPwd;
    }

    public void setLoginPwd(String loginPwd) {
        this.loginPwd = loginPwd;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPlainPassword() {
        return plainPassword;
    }

    public void setPlainPassword(String plainPassword) {
        this.plainPassword = plainPassword;
    }

    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }

    public String getApp() {
        return app;
    }

    public void setApp(String app) {
        this.app = app;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @JsonIgnore
    public Set<String> getPermissions() {
        if (getRoles() == null) {
            return permissions;
        }
        List<Role> items = getRoles();
        int roleSize = items.size();
        for (int i=0; i<roleSize; i++) {
            Role item = items.get(i);
            if (item == null) {
                continue;
            }
            permissions.addAll(item.getPermissionCodes());
        }

        return permissions;
    }

    @JsonIgnore
    public Set<String> getRoleList() {

        if(getRoles()==null){
            return strRoles;
        }
        for(Role role:getRoles()){
            strRoles.add(role.getCode());
        }
        return strRoles;
    }

    public void setPermissions(Set<String> permissions) {
        this.permissions = permissions;
    }


    public List<Role> getRoles() {
        if (CollectionUtils.isEmpty(roles)) {
            return null;
        }

        List<Role> result = new ArrayList<Role>();
        for (Role item : roles) {
            if (item!=null&&!item.getDelflag()) {
                result.add(item);
            }
        }
        return result;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    public Date getRegisterDate() {
        return registerDate;
    }

    public void setRegisterDate(Date registerDate) {
        this.registerDate = registerDate;
    }

    @Override
    public String toString() {
        return "User{" +
                "loginName='" + loginName + '\'' +
                ", loginPwd='" + loginPwd + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}
